﻿Public Class AJAXAutoComplete
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Sub criaConexao()
        Dim cn As New SqlClient.SqlConnection("Data Source=desenv;Initial Catalog=DBSaude;User ID=sa;Password=smtidesenv")
        cn.Open()
        Dim cmd As New SqlClient.SqlCommand
        Dim da As New SqlClient.SqlDataAdapter
        Dim dt As New DataTable
        cmd.Connection = cn
        cmd.CommandText = "Select * from SAUDEUnidadesSaude where UnidadeNome LIKE '%" & Me.TextBox1.Text.Trim & "%' "
        da.SelectCommand = cmd
        da.Fill(dt)
        Me.GridView1.DataSource = dt
        Me.GridView1.DataBind()
        cn.Close()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Me.GridView1.DataSource = ListaNomesUnidades()
        Me.GridView1.DataBind()
    End Sub

    Public Function ListaNomesUnidades() As String()

        Dim cn As New SqlClient.SqlConnection("Data Source=desenv;Initial Catalog=DBSaude;User ID=sa;Password=smtidesenv")
        cn.Open()
        Dim cmd As New SqlClient.SqlCommand
        Dim da As New SqlClient.SqlDataAdapter
        Dim dt As New DataTable
        cmd.Connection = cn
        cmd.CommandText = "Select * from SAUDEUnidadesSaude "
        da.SelectCommand = cmd
        da.Fill(dt)
        Dim nomes(dt.Rows.Count) As String
        Dim i As Integer = 0
        For Each dr As DataRow In dt.Rows
            nomes.SetValue(dr("UnidadeNome").ToString, i)
            i += 1
        Next
        cn.Close()
        Return nomes
    End Function
End Class